package leoner.multithread2;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;

public class PrintTask2<T> implements Callable<T>{
	private Map<String,Object> maps;
	private int sleepTime;
	private static Random generator = new Random();

	private Object targetObject;
	private String targetMethodName;
	private Object[] parameter;
	private Class[] cls;
	public PrintTask2(Object targetObject,String targetMethodName,Map<String,Object> maps,Object... parameter){
		this.targetObject = targetObject;
		this.targetMethodName = targetMethodName;
		this.parameter = parameter;
		cls = new Class[this.parameter.length];
		for(int i=0; i<this.parameter.length;i++){
			cls[i] = this.parameter[i].getClass();
		}
		this.sleepTime = generator.nextInt(5000	);
		this.maps = maps;
	}

	@Override
	public T call() throws Exception {
		Object returnValue = null;
		try {
			System.out.println("============================");
			System.out.println("p length"+this.parameter.length);
			Method method = targetObject.getClass().getMethod(targetMethodName,cls);
			returnValue = method.invoke(targetObject, parameter);
			Thread.sleep(sleepTime);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.printf( "%s done invoke result:\n", returnValue );
		maps.put(targetMethodName, returnValue);
		return (T)returnValue;
	}

}
